<?php

namespace app\mini\model;

use app\mini\job\user\Credit;
use think\facade\Queue;

/**
 * @property int $id
 * @property int $store_user_id
 * @property int $store_id
 * @property int $store_order_id '订单ID',
 * @property int $type
 * @property int $pay_status '支付状态： 1:未支付 ,2:已支付',
 * @property int $pay_time 支付时间
 * @property int $status 状态 1:未出账单 ,2:已出账单',
 * @property int $credit_time 出账时间
 * @property int $money 金额
 */
class StoreUserCredit extends Base
{

    public static function add(StoreOrder $order): bool
    {
        $days = $order->store()->value('credit_days');
        $info = self::create([
            'store_user_id'  => $order->store_user_id,
            'store_id'       => $order->store_id,
            'store_order_id' => $order->id,
            'money'          => $order->storeOrderDetails()->sum('money'),
            'credit_time'    => time() + $days * 24 * 60 * 60,
        ]);
        Queue::later($days * 24 * 60 * 60, Credit::class, ['id' => $info->id],'credit');
        return true;
    }

}